import { mapState, mapActions } from 'vuex';
import { states, actionEvent } from '@/store/modules/api'
import { firstWordToUpperCase } from 'utils';
const stateList = Object.keys(states);
const action = state => `${actionEvent}${firstWordToUpperCase(state)}`;
/**
 * action列表 getActionName
 */
const actionList = (() => {
  return stateList.map(state => action(state));
})();

export default {
  computed: {
    ...mapState('api', stateList)
  },
  data() {
    return {
      states: []
    }
  },
  methods: {
    ...mapActions('api', actionList)
  },
  mounted() {
    const isEmpty = state => {
      const type = states[state];
      const val = this[state];
      switch (type) {
        case 'string':
          return !val;
        case 'object':
          return !Object.keys(val).length;
        case 'array':
          return !val.length;
      }
    }
    this.states.forEach(({ state, cache }) => {
      // 数据为空时主动请求一次
      if (isEmpty(state)) {
        this[action(state)]({ cache, name: state });
      }
    })
  }
}
